package www.openteam.com.model.policy;

import android.support.annotation.NonNull;

import java.util.List;

import www.openteam.com.OTApplication;
import www.openteam.com.base.BaseModel;
import www.openteam.com.bean.ApprovalStatu;
import www.openteam.com.bean.PayableApproval;
import www.openteam.com.exception.ApiException;
import www.openteam.com.subscribe.CommonSubscriber;
import www.openteam.com.transformer.CommonTransformer;

/**
 * Created by kylin on 2016/11/4.
 */

public class PayableApprovalModel extends BaseModel {

    private boolean isQuery = false;

    private boolean isApproval = false;

    private boolean isUnApproval = true;

    //    queryPayableApproval(@Query("vendno")
    // String vendno, @Query("startDate") long startDate,
    // @Query("endDate") long endDate, @Query("pageSize") int PageSize);

    public boolean queryPayableApproval(@NonNull String vendno,
                                        long startDate,
                                        long endDate,
                                        int orderStatus,
                                        String stype, String moreCom,
                                        @NonNull int PageSize, @NonNull final
                                        PayableApprovalOnListener payableApprovalOnListener) {

        httpApiService.queryPayableApproval(vendno, startDate, endDate,orderStatus,stype,moreCom, PageSize)
                .compose(new CommonTransformer<PayableApproval>())
                .subscribe(new CommonSubscriber<PayableApproval>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(PayableApproval payableApproval) {
                        isQuery = true;
                        payableApprovalOnListener.queryPASuccess(payableApproval.getData());
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isQuery = false;
                        payableApprovalOnListener.queryPAFail(e.message);
                    }
                });
        return isQuery;
    }


    public boolean approvalStatu(@NonNull int id, @NonNull
    final PayableOrderAOnListener payableOrderAOnListener) {
        httpApiService.paApprovalUpdate(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isApproval = true;
                        payableOrderAOnListener.approvalPASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isApproval = false;
                        payableOrderAOnListener.approvalPAFail(e.message);
                    }
                });
        return isApproval;
    }


    public boolean unapprovalStatu(@NonNull int id, @NonNull final
    PayableOrderUOnListener payableOrderUOnListener) {
        httpApiService.unPaApprovalUpdate(id)
                .compose(new CommonTransformer<ApprovalStatu>())
                .subscribe(new CommonSubscriber<ApprovalStatu>(OTApplication.getmContext()) {
                    @Override
                    public void onNext(ApprovalStatu approvalStatu) {
                        isUnApproval = true;
                        payableOrderUOnListener.unApprovalPASuccess(approvalStatu);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        isUnApproval = false;
                        payableOrderUOnListener.unApprovalPAFail(e.message);
                    }
                });
        return isUnApproval;
    }

    public interface PayableApprovalOnListener {
        void queryPASuccess(List<PayableApproval.DataEntity> orderApprovals);

        void queryPAFail(String failMsg);
    }

    public interface PayableOrderAOnListener {
        void approvalPASuccess(ApprovalStatu aprovalStatu);

        void approvalPAFail(String failMsg);

    }

    public interface PayableOrderUOnListener {
        void unApprovalPASuccess(ApprovalStatu aprovalStatu);

        void unApprovalPAFail(String failMsg);
    }

}
